package jrdcom.com.jrdmovie.Adapter;


import android.content.Context;
import android.media.Image;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import jrdcom.com.jrdmovie.Glide.JrdImageLoaderProxy;
import jrdcom.com.jrdmovie.JrdHttpMethod.Beans.JrdSubjectBean;
import jrdcom.com.jrdmovie.R;

/**
 * Created by dhcui on 2017/3/26.
 */

public class JrdRecyleAdapter extends RecyclerView.Adapter<JrdRecyleAdapter.JrdRecyleViewHolder>{
    private Boolean jrdIsSmall;
    private List<JrdSubjectBean> jrdListBeans=new ArrayList<>();
    private Context mContext;
    public interface JrdOnItemClick{
        void onClick(View view, int position);
    }
    private JrdOnItemClick mClickListener;
    public JrdRecyleAdapter(List<JrdSubjectBean> list,Context context){
        jrdListBeans  = list;
        jrdIsSmall = true;
        mContext = context;
    }

    public void setOnItemClickListener(JrdOnItemClick clickListener){
        mClickListener = clickListener;
    }
    public JrdRecyleAdapter(Context context){
        mContext = context;
    }
    @Override
    public void onBindViewHolder(JrdRecyleViewHolder holder, final int position) {
        JrdSubjectBean jrdSubjectBean = jrdListBeans.get(position);
        JrdImageLoaderProxy.getInstance().displayImage(jrdSubjectBean.getImages().getMedium(),holder.jrdRecyleImage, mContext);
        holder.jrdRecyleTitle.setText(jrdSubjectBean.getTitle());
        holder.jrdRecyleCasts.setText("演员表："+getCastNames(jrdSubjectBean.getCasts()));
        holder.jrdRecyleDirector.setText("导演:"+getDirNames(jrdSubjectBean.getDirectors()));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mClickListener != null){
                    mClickListener.onClick(v, position);
                }
            }
        });
    }

    public void setSmall(Boolean isSmall){
        jrdIsSmall = isSmall;
    }

    @Override
    public int getItemCount() {
        return jrdListBeans.size();
    }

    //创建ViewHolder
    @Override
    public JrdRecyleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //return null;
        View itemView = null;
        itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_recyle_list,parent,false);
        //itemView.setOnClickListener();
        JrdRecyleViewHolder viewHolder = new JrdRecyleViewHolder(itemView);
        return viewHolder;
    }

    //定义 ViewHolder
    public class JrdRecyleViewHolder extends RecyclerView.ViewHolder{
        private ImageView jrdRecyleImage;
        private TextView jrdRecyleTitle;
        private TextView jrdRecylePoint;
        private TextView jrdRecyleDirector;
        private TextView jrdRecyleCasts;

        public JrdRecyleViewHolder(View itemView){
            super(itemView);
            jrdRecyleImage = (ImageView)itemView.findViewById(R.id.rec_image);
            jrdRecyleTitle = (TextView)itemView.findViewById(R.id.txt_title);
            jrdRecylePoint = (TextView)itemView.findViewById(R.id.txt_point);
            jrdRecyleDirector = (TextView)itemView.findViewById(R.id.txt_director);
            jrdRecyleCasts = (TextView)itemView.findViewById(R.id.txt_casts);
        }
    }

    /**
     * 移动Item
     *
     * @param fromPosition
     * @param toPosition
     */
    public void moveItem(int fromPosition, int toPosition) {
        //做数据的交换
        if (fromPosition < toPosition) {
            for (int index = fromPosition; index < toPosition; index++) {
                Collections.swap(jrdListBeans, index, index + 1);
            }
        } else {
            for (int index = fromPosition; index > toPosition; index--) {
                Collections.swap(jrdListBeans, index, index - 1);
            }
        }
        notifyItemMoved(fromPosition, toPosition);
    }

    /**
     * 滑动Item
     *
     * @param position
     */
    public void removeItem(int position) {
        jrdListBeans.remove(position);//删除数据
        notifyItemRemoved(position);
    }

    public void setArray(List<JrdSubjectBean> list){
        jrdListBeans = list;
        this.notifyDataSetChanged();
    }

    public static String getCastNames(List<JrdSubjectBean.CastsBean> castArray){
        StringBuilder sb = new StringBuilder();
        for (JrdSubjectBean.CastsBean cast : castArray) {
            sb.append(cast.getName() + "/");
        }

        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }

    public static String getDirNames(List<JrdSubjectBean.DirectorsBean> directorsBeen){
        StringBuilder sb = new StringBuilder();
        for (JrdSubjectBean.DirectorsBean cast : directorsBeen) {
            sb.append(cast.getName() + "/");
        }

        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }
}
